/******************************************************
*  Copyright(c) pages.ilovezxl.top
*  FileName :timer.c
*  Author : rivulet.dave
*  Description : 系统定时器
*  Data : 2019-04-23
******************************************************/

/*********************************************************************
 * INCLUDES
 */

#include "stdrope.h"
#include "sallib.h"
#include "salagent.h"

#if (SAL_OSTMR == _ENABLE)

#include "ostmr.h"

#if (SAL_PRODUCER == _SAL_LINUX_)
#include "ostmr_linux.h"
#endif

/*********************************************************************
 *  EXTERNAL VARIABLES
 */

/*********************************************************************
 * MACROS
 */


/*********************************************************************
 * TYPES
 */


/*********************************************************************
 * FUNCTIONS DECLARE
 */


/*********************************************************************
 *  VARIABLES
 */

static OSTIMER_BASE_S *s_pstOSTimerObject = NULL;

/*********************************************************************
 * FUNCTIONS
 */

//  ==== Timer Management Functions ====

/**
 *   @Description: 系统时间定时器模块安装
 *   @param : NA
 *   @return: _OK/_ERR
 */
int32_t osTimerInstall(void)
{
#if (SAL_PRODUCER == _SAL_LINUX_)
    s_pstOSTimerObject = (OSTIMER_BASE_S *)osTimerLinuxObjNew();
#endif
    return _OK;
}

/**
 *   @Description: 系统定时器模块卸载
 *   @param : NA
 *   @return: _OK/_ERR
 */
int32_t osTimerUninstall(void)
{
    s_pstOSTimerObject = NULL;
    return _OK;
}

/// Create and Initialize a timer.
/// \param[in]     func          start address of a timer call back function.
/// \param[in]     type          osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
/// \param[in]     argument      argument to the timer call back function.
/// \param[in]     attr          timer attributes; NULL: default values.
/// \return timer ID for reference by other functions or NULL in case of error.
osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerNew)
    {
        return (osTimerId_t)NULL;
    }

    return s_pstOSTimerObject->pfOSTimerNew(func, type, argument, attr);
}

/// Get name of a timer.
/// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
/// \return name as NULL terminated string.
const char *osTimerGetName (osTimerId_t timer_id)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerGetName)
    {
        return "unknow";
    }

    return s_pstOSTimerObject->pfOSTimerGetName(timer_id);
}

/// Start or restart a timer.
/// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
/// \param[in]     time_ms
/// \return status code that indicates the execution status of the function.
osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t time_ms)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerStart)
    {
        return osError;
    }

    return s_pstOSTimerObject->pfOSTimerStart(timer_id, time_ms);
}

/// Stop a timer.
/// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
/// \return status code that indicates the execution status of the function.
osStatus_t osTimerStop (osTimerId_t timer_id)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerStop)
    {
        return osError;
    }

    return s_pstOSTimerObject->pfOSTimerStop(timer_id);
}

/// Check if a timer is running.
/// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
/// \return 0 not running, 1 running.
uint32_t osTimerIsRunning (osTimerId_t timer_id)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerIsRunning)
    {
        return 0;
    }

    return s_pstOSTimerObject->pfOSTimerIsRunning(timer_id);
}

/// Delete a timer.
/// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
/// \return status code that indicates the execution status of the function.
osStatus_t osTimerDelete (osTimerId_t timer_id)
{
    if (!s_pstOSTimerObject || !s_pstOSTimerObject->pfOSTimerDelete)
    {
        return osError;
    }

    return s_pstOSTimerObject->pfOSTimerDelete(timer_id);
}

#endif  //(SAL_TIMER == _ENABLE)

/************************************** The End Of File **************************************/



